﻿<?php

/*
工具类，用于系统调试
*/

class tool
{
	/*登录*/
	public function login($data)
	{
		$staff = query("staff", "loginName = '" . $data['get']['name'] . "'");
		if (empty($data['get']['name'])) {
			$json['warn'] = "员工姓名为空";
		} elseif (empty($staff['name'])) {
			$json['warn'] = "未找到此员工";
		} else {
			$_SESSION['stid'] = $staff['stid'];
			header("location:" . root . "control/adIndex.html");
			exit(0);
		}
		return json_encode($json, 320);
	}
	
	/*查询全勤奖*/
	public function full($data)
	{
		//$where = " type = '员工结算账户' and text = '2019-12工资结算' ";
		$where = " type = '员工会计账户' and text like '%全勤奖￥200%' and time > '2020-07-09 00:00:00' and time < '2020-07-09 23:00:00' ";
		$sql   = find(array("table" => "record", "where" => $where));
		$x     = 0;
		$html  = "<table>";
		foreach ($sql['array'] as $array) {
			$staff = query("staff", " stid = '$array[typeid]' ");
			$html  .= "
			<tr>
				<td>" . $staff['name'] . "</td>
				<td>" . $array['money'] . "</td>
			</tr>";
			$x++;
		}
		$html .= "</table>";
		return $html;
	}
	
	/*私客转公客*/
	public function publicGuest($data)
	{
		$r = update(array(
			"table" => "kehu",
			"field" => array(
				"stid" => "公客",
			),
			"where" => " workFlow = '放弃' ",
		));
//		$sql = find(array("table" => "kehu","where" => " workFlow = '放弃' "));
//		$html = "";
//		foreach($sql['array'] as $kehu){
//			$r = update(array(
//				"table" => "kehu",
//				"field" => array(
//					"stid" => "公客",
//				),
//				"where" => " khid = '$kehu[khid]' ",
//			));
//			$html .= $kehu['khid']."<br>";
//		}
	}
	
	/*计算现有文件体积*/
	public function size()
	{
//		$sql = find(array("table" => "fileWork","where" => ""));
//		$x = 0;
//		foreach($sql['array'] as $array){
//			$size = round(filesize(serverRoot.$array['src'])/1024/1024,3);
//			echo update(array(
//				"table" => "fileWork",
//				"field" => array(
//					"size" => $size,
//				),
//				"where" => " id = '$array[id]' ",
//			));
//			$x++;
//		}
//		echo "处理了".$x."条记录";
	}
	
	/*清理文件*/
	public function clearImg()
	{
		$x   = 0;
		$sql = find(array("table" => "file", "where" => ""));
		foreach ($sql['array'] as $array) {
			if (!empty($array['src'])) {
				//先把file文件夹重命名为fileNew，然后重建file文件夹，然后移动文件到file下，剩下的就是多余的文件了。
				//$oldUrl = "fileNew/".$array['src'];
				//$day = date("Ym",strtotime($array['updateTime']));
				//建文件夹
//				if(!file_exists(serverRoot."file/".$day)){
//					mkdir(serverRoot."file/".$day);
//				}
				//重命名文件或目录
				if (file_exists(serverRoot . $array['src'])) {
					//echo rename(serverRoot.$oldUrl,serverRoot.$array['src']);
				} else {
					echo $array['id'] . "文件不存在：" . $array['src'] . "<br>";
					$x++;
				}
				//删除
				//fileDelete($oldUrl);
				//返回
				//echo $array['src']."<br>";
				
			}
		}
		echo "处理了" . $x . "条记录";
	}
	
	/*扣社保前工资*/
	public function wages($data)
	{
		$all = 0;
		$sql = find(array("table" => "record", "where" => "type = '员工结算账户' and text = '2020-04工资结算'"));
		foreach ($sql['array'] as $array) {
			$staff = query("staff", "stid = '$array[typeid]'");
			$all   += $array['money'];
			echo $array['money'] . "<br>";
		}
		echo "合计：" . $all;
	}
	
	/*在职天数*/
	public function dutyDay($data)
	{
		$endDay = "2020-01-01 00:00:00";
		$sql    = find(array("table" => "staff", "where" => "state = '在岗' order by entryTime "));
		$html   = head("ad") . "
		<table class='tableMany'>
			<tr>
				<td>姓名</td>
				<td>入职日期</td>
				<td>入职天数</td>
			</tr>
		";
		foreach ($sql['array'] as $staff) {
			$day  = ceil((strtotime($endDay) - strtotime($staff['entryTime'] . " 00:00:00")) / 60 / 60 / 24);
			$html .= "
			<tr>
				<td>" . $staff['name'] . "</td>
				<td>" . $staff['entryTime'] . "</td>
				<td>" . $day . "</td>
			</tr>
			";
		}
		$html .= "</table>";
		return $html;
	}
	
	/*返回*/
	public function html($data)
	{
		//判断主体
		$type = $data['get']['type'];
		if (empty($type)) {
			$html = "执行指令为空";
		} elseif (!method_exists(__CLASS__, $type)) {
			$html = "未知执行指令";
		} else {
			$html = $this->$type($data);
		}
		//返回
		return $html;
	}
}

//导入系统环境
require_once("../control/base.php");
//实例化对象
$tool = new tool();
//打印结果
echo $tool->html(array("get" => $get));
?>
